const Base = require('./base.js');

module.exports = class extends Base {
  constructor(ctx) {
		super(ctx);
		this.category = this.model('store/category');
	}
	async indexAction() {
		let pid = this.get("pid") || 0;
		let categorys = await this.category.getChilds(pid);

		if(this.isPost) {
			return this.success(categorys);
		}
		this.assign("categorys", categorys);
		return this.display();
	}
	
	async selectAction() {
		
		let posts = this.post();
		const data = await this.category.getChilds(posts.pid);
		return this.json(data);
	}
	
	async childAction() {

		let pid = this.post("pid") || 0;
		let childs = await this.category.getChilds(pid);
		this.assign("categorys", childs);
		return this.display();

	}
	
	async addAction() {
		if(this.isGet) {
			let pid = this.get("pid");

			if(pid) {
				let category = await this.category.findOne(pid);
				this.assign("parentCategory", category);

			}
			return this.display();

		}

		let posts = this.post();
console.log(posts);
		let category_id = await this.category.add({
			name: posts.name,
			pid: posts.parentId,
			level: posts.level,
			icon_url: posts.icon_url,
			img_url: posts.img_url,
			keywords: posts.keywords,
			digest: posts.digest,
			title:posts.title
			
			
		});

		if(category_id == 0) {
			return this.fail("添加失败");
		}
		await this.category.setOne(posts.parentId,{
			hasChildren: 1
		});
		return this.success("添加成功");

	}

	async editAction() {
		if(this.isGet) {
			let id = this.get("id");

			if(id) {
				let category = await this.category.findOne(id);

				if(category.pid) {
					let parentCategory = await this.category.findOne(category.pid);
console.log(parentCategory)
					this.assign("parentCategory", parentCategory);

				}

				this.assign("category", category);
				return this.display();
			}

		}

		let posts = this.post();

		let rows = await this.category.setOne(posts.id, {
			name: posts.name,
			pid: posts.parentId,
			level: posts.level,
			icon_url: posts.icon_url,
			img_url: posts.img_url,
			keywords: posts.keywords,
			digest: posts.digest,
			title:posts.title
		});

		if(rows == 0) {
			return this.fail("修改失败");
		}
		return this.success("修改成功");
	}

	async treeAction() {

		let pid = this.get("pid") || 0;
		let categorys = await this.category.getChilds(pid);
		var str = [];
		if(!think.isEmpty(categorys)) {

			categorys.forEach((o) => {
				let obj = {};
				obj.id = o.id;
				obj.text = o.name;
				obj.level = o.level;
				obj.children = o.hasChildren == 1 ? true : false;
				str.push(obj);
			});
		}

		return this.json(str);
	}
	
	async deleteAction(){
		let id = this.post("id");
		let childs = await this.category.where({pid: id}).count();
		if(childs > 0){
			return this.fail("请先删除子分类");
		}
    let pid = await this.category.where({
    	id: id
    }).getField("pid",true);
    
		let rows = await this.category.where({
			id: id
		}).delete();
		if(rows == 0) {
			return this.fail("删除失败")
		}
		
		let pchilds = await this.category.where({pid: pid}).count();
		
		if(pchilds == 0){
			await this.category.setOne(pid,{
				hasChildren: 0
			});
		}
		
		
		return this.success("删除成功")
	}
};
